package com.cssl.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.util.ObjectUtil;
import com.cssl.config.RabbitMqConfig;
import com.cssl.domain.dto.UserAccountDto;
import com.cssl.domain.dto.UsersDto;
import com.cssl.domain.dto.WeiXinLoginDto;
import com.cssl.domain.dto.WeiXinRegistryByPhoneDto;
import com.cssl.service.DtsUserService;
import com.cssl.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 新模板接口
 */
@RestController
@RequestMapping("/users")
@Api("用户接口")
public class UsersController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private DtsUserService service;


    /**
     * 发送短信接口(复制原本的接口)
     *
     * @param req
     * @return
     */
    @PostMapping("/sendMess")
    public Result sendMess(@RequestBody Map<String, Object> req) {
        Result result = service.sendMobileMess(req.get("phone").toString());
        return result;
    }


    /**
     * 手机号注册并登录
     *
     * @param dto
     * @return user对象和saToken
     */
    @PostMapping("/weiXinRegisterByPhone")
    public Result weiXinRegisterByPhone(@RequestBody WeiXinRegistryByPhoneDto dto) {
        System.out.println("进weiXinRegisterByPhone");
        System.out.println(dto);
        //查询手机号是否被注册过
        return service.weiXinRegisterByPhone(dto, request);
    }

    /**
     * 用户名,密码登录
     *
     * @param map 接收的参数 1.username 2.password 3.code
     * @return
     */
    @PostMapping("/weiXinLoginByPwd")
    public Result weiXinLoginByPwd(@RequestBody Map map) {
        return service.weiXinLoginByPwd(map);
    }


    /**
     * 重置密码
     *
     * @param map 1.phone 2.yzm 3.password
     *            验证码错误返回状态码422
     *            密码与原密码一致返回403
     *            修改失败返回400
     *            修改成功200,true
     * @return
     */
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody Map<String, Object> map) {
        return service.resetPassword(map);
    }

    /**
     * 微信登录
     *
     * @param weiXinLoginDto 接收code
     * @return code错误和openId为空返回403
     * 没有此人返回204
     * 请求成功返回200 saToken , user
     */
    @PostMapping("/weiXinLogin")
    public Result weiXinLogin(@RequestBody WeiXinLoginDto weiXinLoginDto) {
        return service.weiXinLogin(weiXinLoginDto, request);
    }

    /**
     * 用户代理申请
     *
     * @param map desc(没用)
     *            userId
     * @return 失败403(参数错误)或400(修改时错误)
     * 成功200
     */
    @PostMapping("/userProxy")
    public Result userProxy(@RequestBody Map<String, Object> map) {
        Object userId = map.get("userId");
        return service.userProxy(userId);
    }

    @PostMapping("/usersList")
    public Result getUsersList(@RequestBody UsersDto usersDto) {
        Map usersList = service.getUsersList(usersDto);
        if (ObjectUtil.isEmpty(usersList.get("list"))) {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, usersList, System.currentTimeMillis());

    }

    @PostMapping("/approveAgency")
    public Result UpdateStatus(@RequestBody UserAccountDto userAccountDto) {
        int i = service.UpdateStatus(userAccountDto.getUserId());
        if (i > 0) {
            return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, null, System.currentTimeMillis());
        }else {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }

    }
    /**
     * 清除用户全部历史搜索记录
     * @return
     */
    @PostMapping("/deleteHistory")
    public Result deleteHistory(){
        Integer loginIdAsInt=null;
        Integer result=0;
        try {
            loginIdAsInt  = StpKit.USER.getLoginIdAsInt();
        }catch (NotLoginException e){
            System.out.println(e);
        }

        if (loginIdAsInt!=null){
            result = service.deleteHistory(loginIdAsInt);
        }
        if (result>0){
            return  Result.result(InfoCode.OK, InfoCode.MESSAGE_OK,result,System.currentTimeMillis());
        }
        return  Result.fail(InfoCode.FAILED, InfoCode.MESSAGE_FAILED,System.currentTimeMillis());
    }

    @Autowired
    private StringRedisTemplate template;
    @PostMapping("/test")
    public Result test(){
        template.opsForSet().add("test:code",1+"");
        template.expire("test:code",10, TimeUnit.SECONDS);
        return Result.result(1001,"fsdf",234,System.currentTimeMillis());
    }
    @PostMapping("/collectTrueOrFalse")
    @ApiOperation(value = "用户收藏商品",notes = "接收type(是否取消收藏),valueId商品id")
    public Result collectTrueOrFalse(@RequestBody Map map){
        return service.collectTrueOrFalse(map);
    }

    @GetMapping("/test2")
    @ApiOperation(value = "测试")
    public void tests(){
        template.opsForValue().set(StorageVariable.ORDERPAYTIME+":"+123,"1243",10,TimeUnit.SECONDS);
    }

}

